<?php
/**
 * 社团
 */
namespace backend\controllers;

use bc\controller\BackendController;
use backend\models\organization\ApplyModel;
use bc\util\backend\Page;
use bc\model\wx\User;
use backend\models\organization\OrganizationModel;
use backend\models\organization\OrganizationUserModel;
use bc\wx\message\Custom;
use bc\util\Util;
use bc\wx\Auth;
use bc\util\AppConst;

class OrganizationApplyController extends BackendController
{
	
	
	/**
	 * 列表页
	 */
	public function actionIndex()
	{
		$model = new ApplyModel();
		$params = 'bc_organization_apply.status = 1 ';
		
		$dateTime = $this->post('date_time', 1);
		
		if($dateTime == 1)//48小时
		{
			$ApplyModelName = ApplyModel::getTableSchema()->name;
			$currentTime = time();
			$beginTime = $currentTime - (48 * 3600);
			$params .= ' AND '.$ApplyModelName.'.create_time >= '.$beginTime;
		}
		
		$page = $this->get('page', 1);
		$model->sort = 'activity_id';
		$list = $model->getApplyList($params, $page, self::PAGE_SIZE, 'apply_id DESC');
		
		$total = $model->getApplyTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		if($list)
		{
			$userModel = new User();
			foreach ($list as $key=>$item)
			{
				$row = $userModel->getRow(['id' => $item['user_id'], 'type' => 1]);
				if($row)
				{
					$list[$key]['user_name'] = $row['nickname'];
					$list[$key]['user_status_name'] = $this->getUserStatusName($item['user_status']);
				}
			}
		}
		
		
		
		$view['list'] = $list;
		$view['pagination'] = $pagination;
		
		return $this->render('index', $view);
	}
	
	
	/**
	 * 编辑
	 */
	public function actionEdit()
	{
		$id = $this->get('id');
		if(!$id)
			$this->redirect('/');
		
		$model = new ApplyModel();
		$row = $model->getRowById($id);
		
		if(!$row)
			$this->redirect('/');
		
		$view['row'] = $row;
		
		return $this->render('edit', $view);
	}
	
	public function actionSave()
	{
		if($this->getRequest()->getMethod() === 'POST' )
		{
			$model = new ApplyModel();
			$id = $this->post('id', 0);
			
			if($id)
			{
// 				$model->modify($data, $id);
			}
			else
			{
// 				$model->add($data);
			}
			$output['msg'] = '操作成功!';
			
		}
		else 
		{
			$output['msg'] = '非法请求!';
		}
		$output['url'] = '/organization-activity/index?organization_id='.$this->organizationId;
		$this->forward($output);
	}
	
	
	public function actionBatch()
	{
		if($this->getRequest()->getMethod() === 'POST' )
		{
			$ids = $this->getRequest()->post('apply_ids');
			if(count($ids) > 0)
			{
				
				$organizationModel = new OrganizationModel();
				$Custom = new Custom();
				$ApplyModel = new ApplyModel();
				$UserModel = new User();
				foreach ($ids as $id)
				{
					
					$apply = $ApplyModel->getRow(['apply_id' => $id]);
					$organization  = $organizationModel->getRowById($apply['organization_id']);
					
					
					$url = STATIC_IMAGE.$organization['join_qrcode'];
					$picUrl = STATIC_IMAGE.$organization['template_image'];
					
					//推送微信内容
					$data['title'] = '点击阅读原文，即可加入社团';
					$data['description'] = '点击阅读原文，长按扫描二维码，即可加入社团';
					$data['url'] = $url;
					$data['picurl'] = $picUrl;
					
					$open_id = $apply['open_id'];
					
					

					$auth = new Auth($this->wechat()->appid, $this->wechat()->secret);
					$accessToken = $auth->accessToken;
					
					$Custom = new Custom();
					$rs = $Custom->send($open_id, $accessToken, $data);
					
					if($rs['errcode'] == 0)//加入用户到列表中
					{
						$user = $UserModel->getRow(['openid' => $apply['open_id']]);
						
						//加入用户到列表中
						$organizationUser = new OrganizationUserModel();
						$organizationUserData['organization_id'] = $apply['organization_id'];
						$organizationUserData['open_id'] = $apply['open_id'];
						$organizationUserData['user_id'] = $user['id'];
						$organizationUserData['user_id_status'] = 1;
						$organizationUserData['create_time'] = time();
						$organizationUser->add($organizationUserData);
						
						//审核状态列表中remove掉
						$apply['status'] = 2;
						$ApplyModel->modify($apply, $apply['apply_id']);
						
						
					}
					
				}
			}
		}
		$this->redirect('/organization-apply');
		
		
	}
	
	
	protected function getUserStatusName($userStatus)
	{
		$userStatusName = '';
		switch ($userStatus)
		{
			case 1:
				$userStatusName = '怀孕中';
				break;
			case 2:
				$userStatusName = '已生宝宝';
				break;
			case 3:
				$userStatusName = '其他';
				break;
		}
		return $userStatusName;
	}
	
}

